public class MyLinkList {
     public ListNode head;
     public ListNode last;
    static class ListNode {
        private int val;
        private ListNode next;
        private ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public ListNode getNext() {
            return next;
        }

        public void setNext(ListNode next) {
            this.next = next;
        }

        public ListNode getPrev() {
            return prev;
        }

        public void setPrev(ListNode prev) {
            this.prev = prev;
        }
    }

    // 2、无头双向链表
        //头插法
        public void addFirst(int data) {
            ListNode node = new ListNode(data);
            if(head == null) {
                head = node;
                last = node;
            }else {
                node.next = head;
                head.prev = node;
                head = node;
            }
    }
        //尾插法
        public void addLast(int data) {
            ListNode node = new ListNode(data);
            if(head == null) {
                head = node;
                last = node;
            } else {
                last.next = node;
                node.prev = last;
                last = last.next;
            }
        }

        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data) {
        if(index < 0 || index > size()) {
            throw new IndexException(index + "下标不合法");
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = head;
        ListNode node = new ListNode(data);
        while(index != 0) {
            cur = cur.next;
            index --;
        }
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
        //查找是否包含关键字key是否在双向链表当中
        public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

        //删除第一次出现关键字为key的节点
        public void remove(int key) {
            ListNode cur  = head;
            while(cur != null) {
                //删除头节点
                if(cur.val == key) {
                    if(cur == head) {
                        head = head.next;
                        if (head != null) {
                            head.prev = null;
                        } else {
                            //只有一个元素
                            last = null;
                        }
                    } else {
                        //删除中间节点或尾巴
                        if(cur.next != null){
                            //中间节点
                            cur.prev.next = cur.next;
                            cur.next.prev = cur.prev;
                        }else {
                            //尾巴
                            last = cur.prev;
                            cur.prev.next = null;
                        }
                    }
                    return;
                }else {
                    cur = cur.next;
                }
            }
        }
        //删除所有值为key的节点
        public void removeAllKey(int key) {
            ListNode cur  = head;
            while(cur != null) {
                //删除头节点
                if(cur.val == key) {
                    if(cur == head) {
                        head = head.next;
                        if (head != null) {
                            head.prev = null;
                        } else {
                            //只有一个元素
                            last = null;
                        }
                    } else {
                        //删除中间节点或尾巴
                        if(cur.next != null){
                            //中间节点
                            cur.prev.next = cur.next;
                            cur.next.prev = cur.prev;
                        }else {
                            //尾巴
                            last = cur.prev;
                            cur.prev.next = null;
                        }
                    }
                    cur = cur.next;
                }else {
                    cur = cur.next;
                }
            }
        }
        //得到单链表的长度
        public int size() {
            int count = 0;
            ListNode cur = head;
            while(cur != null) {
                count ++;
                cur = cur.next;
            }
            return count;
        }
        public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
            System.out.println();
    }

        public void clear() {
            ListNode cur = head;
            while(cur != null) {
                ListNode curNext = cur.next;
                cur.prev = null;
                cur.next = null;
                cur = curNext;
            }
            last = null;
            head = null;
        }
}
